package com.angle.im.server.service;

import com.angle.im.server.domain.Client;
import com.angle.im.server.domain.ImServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.websocket.Session;

/**
 * @author zhangwei
 * @date 2019/4/15 17:52
 * @desc
 */
@Service
@Slf4j
public class ServerService {

    /**
     * 将用户id和serverId进行绑定
     * 以便在分布式情况下能够根据用户id定位服务器,从而获取正确的session
     *
     * @return 为了redis缓存
     */
    @CachePut(cacheNames = "user_server", key = "#platform +':'+#userId")
    public String bindUserId(String platform, String userId, Session session) {
        ImServer.INSTANCE.userClientMap.put(userId, new Client(userId, platform, session));
        ImServer.INSTANCE.localUserNum.incrementAndGet();
        return ImServer.INSTANCE.LOCAL_SERVER_ID;
    }

    /**
     * 通过用户id获取服务器id
     *
     * @return
     */
    @Cacheable(cacheNames = "user_server", key = "#platform +':'+#userId", unless = "#result == ''")
    public String getServerIdByUserId(String platform, String userId) {
        return "";
    }

}
